package com.gdiss.www.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdiss.www.constant.ResultConstant;
import com.gdiss.www.entity.CmsCategory;
import com.gdiss.www.entity.CmsContent;
import com.gdiss.www.mapper.CmsContentMapper;
import com.gdiss.www.service.CmsCategoryService;
import com.gdiss.www.service.CmsContentService;
import com.gdiss.www.vo.ResultVo;
import com.gdiss.www.vo.request.CmsContentRequest;
import com.gdiss.www.vo.response.ListCmsContent;
import com.gdiss.www.vo.response.ListCmsContentResponse;
import com.gdiss.www.vo.response.ShowCmsContentResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  文章服务实现类
 * </p>
 *
 * @author yly
 * @since 2021-11-30
 */
@Service
public class CmsContentServiceImpl implements CmsContentService {
    @Autowired
    CmsContentMapper cmsContentMapper;

    @Autowired
    CmsCategoryService cmsCategoryService;

    @Override
    public ResultVo<? super ShowCmsContentResponse> insertCmsContent(CmsContentRequest cmsContentRequest) {
        //判断分类是否存在
        if (cmsCategoryService.queryCategoryById(cmsContentRequest.getCategoryId())==null){
            return ResultConstant.CATEGORY_ID_NOT_FOUND;
        }
        CmsContent cmsContent = new CmsContent();
        BeanUtils.copyProperties(cmsContentRequest,cmsContent);
        cmsContent.setDisplay(true);
        cmsContent.setStateId(3);
        cmsContent.setPageView(0L);
        cmsContentMapper.insert(cmsContent);
        return queryCmsContent(cmsContent.getId());
    }

    @Override
    public ResultVo<? super ShowCmsContentResponse> queryCmsContent(int id) {
        CmsContent cmsContent = cmsContentMapper.selectById(id);
        if (cmsContent==null)return ResultConstant.CONTENT_ID_NOT_FOUND;
        ShowCmsContentResponse contentResponse = new ShowCmsContentResponse();
        BeanUtils.copyProperties(cmsContent,contentResponse);
        return new ResultVo<>(contentResponse);
    }

    @Override
    public ResultVo<? super ListCmsContentResponse> queryCmsContentByCategory(int curr, int size, int categoryId) {
        //判断分类是否存在
        CmsCategory cmsCategory = cmsCategoryService.queryCategoryById(categoryId);
        if (cmsCategory==null){
            return ResultConstant.CATEGORY_ID_NOT_FOUND;
        }
        Page<CmsContent> page = new Page<>(curr,size);
        QueryWrapper<CmsContent> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",categoryId)
        .eq("display",true).eq("state_id",3);
        if (cmsCategory.getName().equals("学会新闻")){
            wrapper.orderByDesc("create_time");
        }
        else if (cmsCategory.getName().equals("新闻聚焦")){
            wrapper.orderByDesc("page_view");
        }
        else {
            wrapper.orderByDesc("create_time");
        }
        Page<CmsContent> selectPage = cmsContentMapper.selectPage(page, wrapper);
        List<CmsContent> list = selectPage.getRecords();
        List<ListCmsContent> cmsContents = new ArrayList<>();
        for (CmsContent cmsContent : list) {
            ListCmsContent response = new ListCmsContent();
            BeanUtils.copyProperties(cmsContent, response);
            cmsContents.add(response);
        }
        ListCmsContentResponse contentResponse = new ListCmsContentResponse();
        contentResponse.setCmsContents(cmsContents);
        contentResponse.setPageTotal(countPageByCategory(size,categoryId));
        return new ResultVo<>(contentResponse);
    }

    @Override
    public ResultVo<? super ListCmsContentResponse> queryCmsContentBySearch(int curr, int size, String search) {
        Page<CmsContent> page = new Page<>(curr,size);
        QueryWrapper<CmsContent> wrapper = new QueryWrapper<>();
        wrapper.eq("display",true).eq("state_id",3)
                .and(w->w.like("title",search).or().like("detail",search))
                .orderByDesc("create_time");
        Page<CmsContent> selectPage = cmsContentMapper.selectPage(page, wrapper);
        List<CmsContent> list = selectPage.getRecords();
        List<ListCmsContent> cmsContents = new ArrayList<>();
        for (CmsContent cmsContent : list) {
            ListCmsContent response = new ListCmsContent();
            BeanUtils.copyProperties(cmsContent, response);
            cmsContents.add(response);
        }
        ListCmsContentResponse contentResponse = new ListCmsContentResponse();
        contentResponse.setCmsContents(cmsContents);
        contentResponse.setPageTotal((countBySearch(search)-1)/size+1);
        contentResponse.setResultTotal(countBySearch(search));
        return new ResultVo<>(contentResponse);
    }

    @Override
    public int countBySearch(String search) {
        QueryWrapper<CmsContent> wrapper = new QueryWrapper<>();
        wrapper.eq("display",true).eq("state_id",3)
                .and(w->w.like("title",search).or().like("detail",search));
        return cmsContentMapper.selectCount(wrapper);
    }

    @Override
    public int countPageByCategory(int pageSize, int categoryId) {
        QueryWrapper<CmsContent> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",categoryId)
                .eq("display",true).eq("state_id",3);
        Integer count = cmsContentMapper.selectCount(wrapper);
        if (pageSize>0){
            return (count-1)/pageSize+1;
        }
        else {
            return 0;
        }
    }


}
